package com.train.common.utils.object;

import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.train.common.utils.convert.StrConvertUtils;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class BeanUtils {

    public static <T, R> String getFieldName(SFunction<T, R> sFunction) {
        //获取serializedLambda对象
        SerializedLambda serializedLambda;
        try {
            Method method = sFunction.getClass().getDeclaredMethod("writeReplace");
            method.setAccessible(true);
            serializedLambda = (SerializedLambda) method.invoke(sFunction);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return StrConvertUtils.toColumnName(serializedLambda.getImplMethodName());
    }
}
